package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.dto.ConfigDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public Map<String, String> getConfigForm() {
        return sysConfigMapper.getConfigForm();
    }

    @Override
    public List<Map<String, Object>> getAllData() {
        return sysConfigMapper.getAllData();
    }

    @Override
    public List<SysConfig> getChildren() {
        return sysConfigMapper.getChildren();
    }

    @Override
    public List<String> getGroupValues() {
        return sysConfigMapper.getGroupValues();
    }
    @Override
    public List<Object> allList() {
        List<Object> list = new ArrayList<>();
        LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getTabValue, "base");
        List<SysConfig> baseLists = this.list(lqw);

        //四级map集合
        HashMap<String, Object> baseMaps = new HashMap<>();
        HashMap<String, Object> bottomMaps = new HashMap<>();
        HashMap<String, Object> otherMaps = new HashMap<>();

        //五级list集合
        ArrayList<ConfigDto> baseList = new ArrayList<>();
        ArrayList<ConfigDto> bottomList = new ArrayList<>();
        ArrayList<ConfigDto> otherList = new ArrayList<>();

        Map<String, String> configValue1 = new HashMap<>();
        Map<String, String> configValue2 = new HashMap<>();
        configValue1.put("label", "是");
        configValue1.put("value", "1");
        configValue2.put("label", "否");
        configValue2.put("value", "2");
        ArrayList<Map<String, String>> configValue = new ArrayList<>();
        configValue.add(configValue1);
        configValue.add(configValue2);

        List<ConfigDto> configDtoList = baseLists.stream().map(config -> {
            ConfigDto configDto = new ConfigDto();
            BeanUtils.copyProperties(config, configDto);
            if (config.getConfigValue().length() != 0) {
                configDto.setConfigValue(configValue);
            }
            return configDto;
        }).collect(Collectors.toList());
        //对基础内容进行区分,分别装入集合
        for (ConfigDto sysConfig : configDtoList) {
            if (sysConfig.getGroupValue().equals("站点配置")) {
                baseList.add(sysConfig);
            } else if (sysConfig.getGroupValue().equals("底部信息")) {
                bottomList.add(sysConfig);
            } else
                otherList.add(sysConfig);
        }
        //基础配置添加
        List<Object> base = new ArrayList<>();
        baseMaps.put("name", "站点设置");
        baseMaps.put("data", baseList);
        bottomMaps.put("name", "底部信息");
        bottomMaps.put("data", bottomList);
        otherMaps.put("name", "其他设置");
        otherMaps.put("data", otherList);
        base.add(baseMaps);
        base.add(bottomMaps);
        base.add(otherMaps);

        //处理系统配置信息
        lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getTabValue, "system");
        List<SysConfig> systemLists = this.list(lqw);
        ArrayList<Object> system = new ArrayList<>();


        //系统配置的容器
        HashMap<String, Object> systemMaps = new HashMap<>();
        systemMaps.put("name", "管理系统配置");
        systemMaps.put("data", systemLists);
        system.add(systemMaps);

        //处理其他配置信息
        lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getTabValue, "custom");
        List<SysConfig> customLists = this.list(lqw);
        //对其他内容信息进行区分,分别装入集合
        ArrayList<SysConfig> other1 = new ArrayList<>();
        ArrayList<SysConfig> other2 = new ArrayList<>();
        for (SysConfig sysConfig : customLists) {
            if (sysConfig.getGroupValue().equals("通知公告")) {
                other1.add(sysConfig);
            } else {
                other2.add(sysConfig);
            }
        }

        //其他配置添加
        List<Object> other = new ArrayList<>();

        HashMap<String, Object> noticeMap = new HashMap<>();
        HashMap<String, Object> photoMap = new HashMap<>();
        noticeMap.put("name", "通知公告");
        noticeMap.put("data", other1);
        photoMap.put("name", "通知图片");
        photoMap.put("data", other2);
        other.add(noticeMap);
        other.add(photoMap);

        //三级map集合
        Map<String, Object> baseMap = new HashMap<>();
        Map<String, Object> systemMap = new HashMap<>();
        Map<String, Object> customMap = new HashMap<>();

        baseMap.put("name", "基础配置");
        baseMap.put("key", "base");
        baseMap.put("children", base);

        systemMap.put("name", "系统配置");
        systemMap.put("key", "system");
        systemMap.put("children", system);

        customMap.put("name", "其他配置");
        customMap.put("key", "custom");
        customMap.put("children", other);

        //二级list集合
        list.add(baseMap);
        list.add(systemMap);
        list.add(customMap);
        return list;
    }

    @Override
    public List<Object> allData() {

        List<SysConfig> sysConfigs = this.list();
        List<Object> allData = sysConfigs.stream().map(sysConfig -> {
            SysConfigDto sysConfigDto = new SysConfigDto();
            BeanUtils.copyProperties(sysConfig, sysConfigDto);
            return sysConfigDto;
        }).collect(Collectors.toList());


        return allData;
    }

    @Override
    public Map<String, String> configForm() {
        Map<String, String> configForm = new HashMap<>();
        List<SysConfig> sysConfigs = this.list();
        for (SysConfig sysConfig : sysConfigs) {
            configForm.put(sysConfig.getAttrKey(), sysConfig.getAttrValue());
        }
        return configForm;
    }

    @Override
    public Map<String, Object> configRules() {
        Map<String, Object> configRules = new HashMap<>();
        LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getAttrKey, "site_name");
        SysConfig sysConfig = this.getOne(lqw);
        configRules.put(sysConfig.getAttrKey(), sysConfig.getValidator());
        return configRules;
    }


}
